//
//  WPDiagnosisHeaderView.m
//  wobingwoyi
//
//  Created by WGP on 2016/11/30.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPDiagnosisHeaderView.h"
#import "WPMRModel.h"

@interface WPDiagnosisHeaderView ()

@property(nonatomic,strong)UILabel *firstLabel;
@property(nonatomic,strong)UIImageView *line;
@property(nonatomic,strong)UIImageView *tipImageView;
@property(nonatomic,strong)UIView *containerView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)YYLabel *diseaseLabel;
@property(nonatomic,strong)UILabel *doctorLabel;
@property(nonatomic,strong)UILabel *hospitalLabel;
@property(nonatomic,strong)UILabel *sexLabel;
@property(nonatomic,strong)UILabel *ageLabel;
@property(nonatomic,strong)UIButton *ZXBtn;
@property(nonatomic,strong)UIButton *stateButton;

@property(nonatomic,strong)UIImageView *starImageView1;
@property(nonatomic,strong)UIImageView *starImageView2;
@property(nonatomic,strong)UIImageView *starImageView3;
@property(nonatomic,strong)UIImageView *starImageView4;
@property(nonatomic,strong)UIImageView *starImageView5;
@property(nonatomic,strong)UIImageView *starImageView6;

@end
@implementation WPDiagnosisHeaderView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 275)])
    {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self tipImageView];
    [self firstLabel];
    [self containerView];
    [self imageView];
    [self label];
}

-(UIImageView *)tipImageView
{
    if (_tipImageView == nil)
    {
        _tipImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"example_tip"]];
        _tipImageView.hidden = YES;
        [self addSubview:_tipImageView];

        [_tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(10);
        }];
    }
    return _tipImageView;
}

-(UILabel *)firstLabel
{
    if (_firstLabel == nil)
    {
        _firstLabel = [UILabel new];
        _firstLabel.text = @"一诊诊断凭据";
        _firstLabel.textColor = GLobalFontColor;
        _firstLabel.textAlignment = NSTextAlignmentCenter;
        _firstLabel.font = [UIFont systemFontOfSize:15];
        _firstLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:_firstLabel];
        _firstLabel.frame = CGRectMake(0, 10, SCREEN_WIDTH, 44);
        
        UIImageView *line = [UIImageView lineImageView];
        self.line = line;
        [self addSubview:line];
        line.frame = CGRectMake(0, 10, SCREEN_WIDTH, 0.5);
    }
    return _firstLabel;
}

-(UIView *)containerView
{
    if (_containerView == nil)
    {
        _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 54, SCREEN_WIDTH, 220-44)];
        _containerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_containerView];
        
        [self setLines];
        [self setupLabels];
        
    }
    return _containerView;
}

-(UIImageView *)imageView
{
    if (_imageView == nil)
    {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zhenduan"]];
        [self addSubview:_imageView];
        
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            //            make.width.height.mas_equalTo(15);
            make.top.equalTo(self.containerView.mas_bottom).offset(15);
        }];
        
    }
    return _imageView;
}

-(UILabel *)label
{
    if (_label == nil)
    {
        _label = [UILabel new];
        _label.font = [UIFont systemFontOfSize:15];
        _label.textColor = GLobalFontColor;
        _label.text = @"诊断单";
        [self addSubview:_label];
        
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.imageView);
            make.left.equalTo(self.imageView.mas_right).offset(10);
        }];
    }
    return _label;
}

-(void)setLines
{
    UIImageView *firstLine = [UIImageView lineImageView];
    UIImageView *secondLine = [UIImageView lineImageView];
    UIImageView *thirdLine = [UIImageView lineImageView];
    UIImageView *fourthLine = [UIImageView lineImageView];
    UIImageView *fifthLine = [UIImageView lineImageView];
    
    [_containerView addSubview:firstLine];
    [_containerView addSubview:secondLine];
    [_containerView addSubview:thirdLine];
    [_containerView addSubview:fourthLine];
    [_containerView addSubview:fifthLine];
    
    [firstLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(_containerView);
        make.height.mas_equalTo(0.5);
    }];
    
    [secondLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_containerView).offset(10);
        make.right.height.equalTo(firstLine);
        make.top.equalTo(_containerView).offset(44);
    }];
    
    [thirdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(secondLine);
        make.top.equalTo(_containerView).offset(88);
    }];
    
    [fourthLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(secondLine);
        make.top.equalTo(_containerView).offset(132);
    }];
    
    [fifthLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(firstLine);
        make.top.equalTo(_containerView).offset(176);
    }];
}

-(void)setupLabels
{
    YYLabel *firstLabel = [YYLabel new];
    self.diseaseLabel = firstLabel;
    UILabel *secondLabel = [self labelWith:@"诊治医生:"];
    self.doctorLabel = secondLabel;
    UILabel *thirdLabel = [self labelWith:@"治疗医院:"];
    self.hospitalLabel = thirdLabel;
    UILabel *fourthLabel = [self labelWith:@"患者性别:"];
    self.sexLabel = fourthLabel;
    UILabel *fifthLabel = [self labelWith:@"患者年龄:"];
    self.ageLabel = fifthLabel;
    self.stateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.stateButton setImage:[UIImage imageNamed:@"treamentState"] forState:UIControlStateNormal];
    [self.stateButton setTitleColor:GLobalFontColor forState:UIControlStateNormal];
    self.stateButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    UIImageView *lineView = [UIImageView new];
    lineView.backgroundColor = GLobalLineColor;
    
    UIImageView *firstLine = [UIImageView lineImageView];
    
    [_containerView addSubview:firstLabel];
    [_containerView addSubview:secondLabel];
    [_containerView addSubview:thirdLabel];
    [_containerView addSubview:fourthLabel];
    [_containerView addSubview:fifthLabel];
    [_containerView addSubview:self.stateButton];
    
    [_containerView addSubview:lineView];
    [_containerView addSubview:firstLine];
    
    CGSize size = [secondLabel.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:15];
    CGFloat margin = (44-size.height)/2.0;
    
    CGFloat wScale = SCREEN_WIDTH/375.0;
    [firstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_containerView).offset(10);
        make.right.equalTo(_containerView.mas_centerX).offset(-10);
        make.top.equalTo(_containerView).offset(margin);
    }];
    
    [secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firstLabel);
        make.top.equalTo(firstLabel.mas_bottom).offset(2*margin);
    }];
    
    [thirdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firstLabel);
        make.top.equalTo(secondLabel.mas_bottom).offset(2*margin);
    }];
    
    [fourthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firstLabel);
        make.top.equalTo(thirdLabel.mas_bottom).offset(2*margin);
    }];
    
    [fifthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.with.equalTo(fourthLabel);
        make.left.equalTo(_containerView.mas_centerX).offset(10);
    }];
    
    [firstLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(24);
        make.centerX.equalTo(_containerView.mas_centerX).offset(50*wScale);
        make.centerY.equalTo(firstLabel);
    }];
    
    CGFloat stateBtnMargin = 50*wScale + (SCREEN_WIDTH*0.5-50*wScale)/2;
    [self.stateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_containerView.mas_centerX).offset(stateBtnMargin);
        make.centerY.equalTo(firstLabel);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(24);
        make.centerX.equalTo(_containerView.mas_centerX);
        make.centerY.equalTo(fifthLabel);
    }];
    
    self.ZXBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.ZXBtn setBackgroundImage:[UIImage imageNamed:@"ZX_normal"] forState:UIControlStateNormal];
    [self.ZXBtn setBackgroundImage:[UIImage imageNamed:@"ZX_highlight"] forState:UIControlStateHighlighted];
    [self.ZXBtn setTitle:@"西医" forState:UIControlStateNormal];
    [self.ZXBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
    self.ZXBtn .titleLabel.font = [UIFont systemFontOfSize:12];
    [_containerView addSubview:self.ZXBtn];
    
    UIImage *image = [UIImage imageNamed:@"ZX_normal"];
    [self.ZXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.stateButton);
        make.right.equalTo(firstLine).offset(-10);
        make.width.mas_equalTo(image.size.width);
    }];
    
    self.starImageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mandatory_star"]];
    self.starImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mandatory_star"]];
    self.starImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mandatory_star"]];
    self.starImageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mandatory_star"]];
    self.starImageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mandatory_star"]];
    self.starImageView6 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mandatory_star"]];
    [_containerView addSubview:self.starImageView1];
    [_containerView addSubview:self.starImageView2];
    [_containerView addSubview:self.starImageView3];
    [_containerView addSubview:self.starImageView4];
    [_containerView addSubview:self.starImageView5];
    [_containerView addSubview:self.starImageView6];
    
    [self.starImageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_containerView).offset(-10);
        make.centerY.equalTo(firstLabel);
    }];
    [self.starImageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_containerView).offset(-10);
        make.centerY.equalTo(secondLabel);
    }];
    [self.starImageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_containerView).offset(-10);
        make.centerY.equalTo(thirdLabel);
    }];
    [self.starImageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_containerView).offset(-10);
        make.centerY.equalTo(fourthLabel);
    }];
    [self.starImageView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(firstLine.mas_left).offset(-10);
        make.centerY.equalTo(firstLabel);
    }];
    [self.starImageView6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineView.mas_left).offset(-10);
        make.centerY.equalTo(fourthLabel);
    }];
}

-(void)setIsExample:(BOOL)isExample
{
    _isExample = isExample;
    
    
    if (isExample)
    {
        UIImage *image = [UIImage imageNamed:@"example_tip"];
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 275+10+image.size.height);
        self.containerView.frame = CGRectMake(0, 20+image.size.height + 44, SCREEN_WIDTH, 220-44);
        self.firstLabel.frame = CGRectMake(0, 20+image.size.height, SCREEN_WIDTH, 44);
        self.line.frame = CGRectMake(0, 20+image.size.height, SCREEN_WIDTH, 0.5);
        self.tipImageView.hidden = NO;
        self.starImageView1.hidden = NO;
        self.starImageView2.hidden = NO;
        self.starImageView3.hidden = NO;
        self.starImageView4.hidden = NO;
        self.starImageView5.hidden = NO;
        self.starImageView6.hidden = NO;
    }
    else
    {
        self.tipImageView.hidden = YES;
        self.starImageView1.hidden = YES;
        self.starImageView2.hidden = YES;
        self.starImageView3.hidden = YES;
        self.starImageView4.hidden = YES;
        self.starImageView5.hidden = YES;
        self.starImageView6.hidden = YES;
    }

}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    NSString *diseaseLabelText = [NSString stringWithFormat:@"疾病名称:  %@",model.name];
    if (self.isExample)
    {
        self.diseaseLabel.text = diseaseLabelText;
        self.diseaseLabel.font = [UIFont systemFontOfSize:15];
        self.diseaseLabel.textColor = GLobalFontColor;
    }
    else
    {
        NSRange range = [diseaseLabelText rangeOfString:model.name];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:diseaseLabelText];
        text.yy_font = [UIFont systemFontOfSize:15];
        text.yy_color = GLobalFontColor;
        
        [text yy_setTextHighlightRange:range color:GlobalColor backgroundColor:[UIColor whiteColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            
            if(self.diseaseInstructionBlock)
            {
                self.diseaseInstructionBlock();
            }
        }];
        self.diseaseLabel.attributedText = text;
    }
    
     self.doctorLabel.text = [NSString stringWithFormat:@"诊治医生:  %@",model.doctor];
     self.hospitalLabel.text = [NSString stringWithFormat:@"治疗医院:  %@",model.hospital];
     self.sexLabel.text = [NSString stringWithFormat:@"患者性别:  %@",model.sex];
     self.ageLabel.text = [NSString stringWithFormat:@"患者年龄:  %zd",model.age];
    [self.stateButton setTitle:[NSString stringWithFormat:@"  %@",model.cureStatus] forState:UIControlStateNormal];
    [self.ZXBtn setTitle:self.model.type forState:UIControlStateNormal];
}

-(UILabel *)labelWith:(NSString *)title
{
    UILabel *label = [UILabel new];
    label.text = title;
    label.textColor = GLobalFontColor;
    label.font = [UIFont systemFontOfSize:15];
    return label;
}

@end
